#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
# vim:tabstop=4
#
# Copyright (c) 2010 by xkhhdx
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys, re
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

import logging
from google.appengine.ext import db
from google.appengine.api import memcache

import configure
import parse
import fetcher
import updaters

if not configure.cron_enable:
    sys.exit(0)

# 下面这个标志决定是否同步图片的地址
#parse.add_pic_url = False

print "Content-Type: text/html"
print ""

class MicroblogItem(db.Model):
    u""" 数据库查询模型 这里保存则最新的条目 """

    id=db.StringProperty()
    create_time = db.DateTimeProperty(auto_now_add=True)

def query_latest():
    u""" 得到最新的条目的 ID 这里查询数据库 """

    msgs=db.GqlQuery("SELECT * FROM MicroblogItem ORDER BY create_time DESC")
    if msgs.count():
        #lastestitem = MicroblogItem()
        #lastestitem.id = "{'sina':'0','twitter':'0'}" # regenerate it
        #return lastestitem

        return msgs[0]
    else:
        lastestitem = MicroblogItem()
        lastestitem.id = "{'sina':'0','twitter':'0'}"
        lastestitem.put()
        return lastestitem

def get_latest():
    u""" 得到最新的条目的 ID
         如果 memcache 中有则用 memcache 否则查询数据库 """

    data = memcache.get("lastestitem")
    if data is not None:
        ##logging.info("using memcache lastestitem")
        ##logging.info("    `data` = " + `data`)
        return data
    else:
        ##logging.info("querying db lastestitem")
        data = query_latest()
        memcache.add("lastestitem", data, 3630)
        return data

def tuner(user_id, since_id, source_sp = "sina"):
    u""" 同步 """

    try:
    	# 注意 since_id 可能会没有 source_sp 字段（增加来源时）
        content, src = fetcher.fetch(user_id, since_id[source_sp], source_sp)
        src = fetcher.sourcespname[src]
        m = parse.parse_tweet(content)
        print "`m` =" , `m`
    except Exception,e:
        ###logging.info( "ERROR!" + e.what() )
        raise e
        #pass
    else:
        for x in reversed(m):
            id = x['id']

            if str(id) > str(since_id[source_sp]):
                x = parse.cook_tweet(x, src, 140)
                if re.match( r"[^-].*$", x['text'] ): # 在这里我们可以设置匹配某（些）正则以实现部分同步
                	logging.info ("syncing text = " +x['text'])
                    for usermanage in configure.load_usermanage():
                        updater = updaters.microblogs[usermanage.serviceprovider]

                        for property in updater.__properties__:
                            try:
                                properties = eval(usermanage.config) #config.usermanage [ updater.__serviceprovider__ ]
                            except Exception,e:
                                print "ERROR" + e, "<br/>"
                                properties = None

                            if not properties: # check whether to remove this block or not(if nessary)...
                                properties = {}
                                print u"请输入", updater.__properties__[property], u"： "
                                properties[ property ] = raw_input()
                        # end for property in updater.__properties__

                        x = parse.cook_tweet(x, src, updater.__lengthlimit__)
                        text=x['text']

                        print "<li>",str(id), str(since_id), str(text), "</li><br />"
                        print "updating to", updater.__serviceprovider__, "..."

                        r, s = updater.update(properties, text)
                        print "r, s=", r, s,"<br/>"
                    # end for usermanage in configure.load_usermanage()

                    since_id[source_sp] = str(id)
                    lastestitem.id = `since_id`
                    lastestitem.put()

                    if memcache.get("lastestitem") is not None:
                        memcache.set("lastestitem", lastestitem, 3630)
                    else:
                        memcache.add("lastestitem", lastestitem, 3630)

                # end if
            # end if
        # end for


lastestitem = get_latest()
since_id = eval(lastestitem.id)

if __name__ == '__main__':
    ##logging.info("main")
    print "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>synchronizing...</title></head><body><ol>", "<br/>"

    tuner(configure.configs["sinatid"], since_id, "sina")
    try:
        configure.configs["twitterid"]
    except Exception,e :
    	logging.info("tunering twitter ERROR" +`e`)
    else:
    	logging.info("tunering twitter")
    	print "tunering twitter"
        tuner(configure.configs["twitterid"], since_id, "twitter")
        
    print "<br/>done<br/></ol></body></html>"
